package com.projectsquirrel;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.Toast;

import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class NewObservation6 extends Activity  {
	
	private static final int PREFERENCES_GROUP_ID = 11;
	private static final int PAGE_2 = 12;
	private static final int PAGE_3 = 13;
	private static final int PAGE_4 = 14;
	private static final int PAGE_5 = 15;
	private static final int PAGE_6 = 16;
	private static final int PAGE_7 = 17;
	
	private LocationManager myManager;

	private Observation newObs;
	    

    private Button nextObsButton6;
    private TextView completitionStatus;
    private Context context;
    private EditText feedComments;
    private RadioGroup bird_Feed_Group;
    private RadioGroup human_Feed_Group;
    private RadioGroup garbage_Feed_Group;
    private RadioGroup other_Feed_Group;
    private RadioGroup plant_Feed_Group;
    private SharedPreferences pref;
    private CheckBox check1; 
    private CheckBox check2; 
    private CheckBox check3; 
    private CheckBox check4; 

    @Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.observationformed);//layout 1.5
	    ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	    ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingsScreenIcon);
	    pref = getSharedPreferences("AppSettings",MODE_PRIVATE);
	    String email = pref.getString("userEmail", null);
	    newObs = Observation.getInstance(email);
	    nextObsButton6 = (Button) findViewById(R.id.nextObsScreen6);
	    
	    bird_Feed_Group = (RadioGroup) findViewById(R.id.bird_Feed_Group);
	    human_Feed_Group = (RadioGroup) findViewById(R.id.human_Feed_Group);
	    garbage_Feed_Group = (RadioGroup) findViewById(R.id.garbage_Feed_Group);
	    other_Feed_Group = (RadioGroup) findViewById(R.id.other_Feed_Group);
	    plant_Feed_Group = (RadioGroup) findViewById(R.id.plant_Feed_Group);
	    check1 = (CheckBox) findViewById(R.id.checkbox1);
	    check2 = (CheckBox) findViewById(R.id.checkbox2);
	    check3 = (CheckBox) findViewById(R.id.checkbox3);
	    check4 = (CheckBox) findViewById(R.id.checkbox4);

	    
	    /* This does not update Observation.java, if you want a default entry you must also enter it in the class manually*/
	    
	    String bird = newObs.getFeedFromBirdFeeder();
	    if(bird.equals("NEVER")) bird_Feed_Group.check(R.id.bird_Feeders_Never);
	    if(bird.equals("RARE")) bird_Feed_Group.check(R.id.bird_Feeders_Rare);
	    if(bird.equals("OFTEN")) bird_Feed_Group.check(R.id.bird_Feeders_Often);
	    if(bird.equals("REGULAR")) bird_Feed_Group.check(R.id.bird_Feeders_Regular);
	    String human = newObs.getFeedFromHandouts();
	    if(human.equals("NEVER")) human_Feed_Group.check(R.id.human_Never);
	    if(human.equals("RARE")) human_Feed_Group.check(R.id.human_Rare);
	    if(human.equals("OFTEN")) human_Feed_Group.check(R.id.human_Often);
	    if(human.equals("REGULAR")) human_Feed_Group.check(R.id.human_Regular);
	    String garbage = newObs.getFeedFromGarbage();
	    if(garbage.equals("NEVER")) garbage_Feed_Group.check(R.id.garbage_Never);
	    if(garbage.equals("RARE")) garbage_Feed_Group.check(R.id.garbage_Rare);
	    if(garbage.equals("OFTEN")) garbage_Feed_Group.check(R.id.garbage_Often);
	    if(garbage.equals("REGULAR")) garbage_Feed_Group.check(R.id.garbage_Regular);
	    String plant = newObs.getFeedFromTrees();
	    if(plant.equals("NEVER")) plant_Feed_Group.check(R.id.trees_Never);
	    if(plant.equals("RARE")) plant_Feed_Group.check(R.id.trees_Rare);
	    if(plant.equals("OFTEN")) plant_Feed_Group.check(R.id.trees_Often);
	    if(plant.equals("REGULAR")) plant_Feed_Group.check(R.id.trees_Regular);
	    String other = newObs.getFeedFromOther();
	    if(other.equals("NEVER")) other_Feed_Group.check(R.id.other_Never);
	    if(other.equals("RARE")) other_Feed_Group.check(R.id.other_Rare);
	    if(other.equals("OFTEN")) other_Feed_Group.check(R.id.other_Often);
	    if(other.equals("REGULAR")) other_Feed_Group.check(R.id.other_Regular);
	    /*   ((RadioButton) findViewById(R.id.bird_Feeders_Never)).performClick();
	    ((RadioButton) findViewById(R.id.human_Never)).performClick();
	    ((RadioButton) findViewById(R.id.garbage_Never)).performClick();
	    ((RadioButton) findViewById(R.id.trees_Never)).performClick();
	    ((RadioButton) findViewById(R.id.other_Never)).performClick();
	    //Like So
	    newObs.setBirdFeed("NEVER");
	    newObs.setHumanFeed("NEVER");
	    newObs.setGarbageFeed("NEVER");
	    newObs.setPlantFeed("NEVER");
	    newObs.setOtherFeed("NEVER");
	    */
	    
	    check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.bird_Feeders_Never)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.human_Never)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.garbage_Never)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.trees_Never)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.other_Never)).setChecked(true);
		        	check2.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
		        	check4.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(bird_Feed_Group.getCheckedRadioButtonId()==R.id.bird_Feeders_Never)
	        		{
	        	    bird_Feed_Group.clearCheck();
	        		}
	        		if(garbage_Feed_Group.getCheckedRadioButtonId()==R.id.garbage_Never)
	        		{
	        			garbage_Feed_Group.clearCheck();
	        		}
	        		if(human_Feed_Group.getCheckedRadioButtonId()==R.id.human_Never)
	        		{
	        			human_Feed_Group.clearCheck();
	        		}
	        		if(plant_Feed_Group.getCheckedRadioButtonId()==R.id.trees_Never)
	        		{
	        			plant_Feed_Group.clearCheck();
	        		}
	        		if(other_Feed_Group.getCheckedRadioButtonId()==R.id.other_Never)
	        		{
	        	    other_Feed_Group.clearCheck();
	        		}
		    	}
	        }
	    });
	  	    
	    check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.bird_Feeders_Rare)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.human_Rare)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.garbage_Rare)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.trees_Rare)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.other_Rare)).setChecked(true);
		        	check1.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
		        	check4.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(bird_Feed_Group.getCheckedRadioButtonId()==R.id.bird_Feeders_Rare)
	        		{
	        	    bird_Feed_Group.clearCheck();
	        		}
	        		if(garbage_Feed_Group.getCheckedRadioButtonId()==R.id.garbage_Rare)
	        		{
	        			garbage_Feed_Group.clearCheck();
	        		}
	        		if(human_Feed_Group.getCheckedRadioButtonId()==R.id.human_Rare)
	        		{
	        			human_Feed_Group.clearCheck();
	        		}
	        		if(plant_Feed_Group.getCheckedRadioButtonId()==R.id.trees_Rare)
	        		{
	        			plant_Feed_Group.clearCheck();
	        		}
	        		if(other_Feed_Group.getCheckedRadioButtonId()==R.id.other_Rare)
	        		{
	        	    other_Feed_Group.clearCheck();
	        		}
		    	}
	        }
	    });
	    check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.bird_Feeders_Often)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.human_Often)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.garbage_Often)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.trees_Often)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.other_Often)).setChecked(true);
		        	check2.setChecked(false);	    	        		
		        	check1.setChecked(false);	    	        		
		        	check4.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(bird_Feed_Group.getCheckedRadioButtonId()==R.id.bird_Feeders_Often)
	        		{
	        	    bird_Feed_Group.clearCheck();
	        		}
	        		if(garbage_Feed_Group.getCheckedRadioButtonId()==R.id.garbage_Often)
	        		{
	        			garbage_Feed_Group.clearCheck();
	        		}
	        		if(human_Feed_Group.getCheckedRadioButtonId()==R.id.human_Often)
	        		{
	        			human_Feed_Group.clearCheck();
	        		}
	        		if(plant_Feed_Group.getCheckedRadioButtonId()==R.id.trees_Often)
	        		{
	        			plant_Feed_Group.clearCheck();
	        		}
	        		if(other_Feed_Group.getCheckedRadioButtonId()==R.id.other_Often)
	        		{
	        	    other_Feed_Group.clearCheck();
	        		}
		    	}
	        }
	    });
	    check4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	        {
	        	if(isChecked)
	        	{
	        	    ((RadioButton) findViewById(R.id.bird_Feeders_Regular)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.human_Regular)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.garbage_Regular)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.trees_Regular)).setChecked(true);
	        	    ((RadioButton) findViewById(R.id.other_Regular)).setChecked(true);
		        	check2.setChecked(false);	    	        		
		        	check3.setChecked(false);	    	        		
		        	check1.setChecked(false);	    	        		
	        	}
	        	else
	        	{
	           		if(bird_Feed_Group.getCheckedRadioButtonId()==R.id.bird_Feeders_Regular)
	        		{
	        	    bird_Feed_Group.clearCheck();
	        		}
	        		if(garbage_Feed_Group.getCheckedRadioButtonId()==R.id.garbage_Regular)
	        		{
	        			garbage_Feed_Group.clearCheck();
	        		}
	        		if(human_Feed_Group.getCheckedRadioButtonId()==R.id.human_Regular)
	        		{
	        			human_Feed_Group.clearCheck();
	        		}
	        		if(plant_Feed_Group.getCheckedRadioButtonId()==R.id.trees_Regular)
	        		{
	        			plant_Feed_Group.clearCheck();
	        		}
	        		if(other_Feed_Group.getCheckedRadioButtonId()==R.id.other_Regular)
	        		{
	        	    other_Feed_Group.clearCheck();
	        		}
		    	}
	     	    	
	        }
	    });

	    feedComments = (EditText) findViewById(R.id.feedComments);
	    feedComments.setText(newObs.getFeedFromComments());
	    completitionStatus = (TextView) findViewById(R.id.textView2);
	    completitionStatus.setText("82% complete");
	    context = getApplicationContext();
	   
	    

	    
	    
	  	    
	    
	    //setup the gps manager
	    myManager = (LocationManager) getSystemService(LOCATION_SERVICE);	
	   
	    
	    
	    bird_Feed_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.bird_Feeders_Never:
	        		newObs.setBirdFeed("NEVER");
	        		break;
	        	case R.id.bird_Feeders_Rare:
	        		newObs.setBirdFeed("RARE");
	        		break;
	        	case R.id.bird_Feeders_Often:
	        		newObs.setBirdFeed("OFTEN");
	        		break;
	        	case R.id.bird_Feeders_Regular:
	        		newObs.setBirdFeed("REGULAR");
	        		break;
	        	default:
//	        		Toast.makeText(context, "Error Bird Feed Group", 2000).show();
	        		break;
	        	}     	
	        	
	        	
	        }
	    });
	    
	    human_Feed_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.human_Never:
	        		newObs.setHumanFeed("NEVER");
	        		break;
	        	case R.id.human_Rare:
	        		newObs.setHumanFeed("RARE");
	        		break;
	        	case R.id.human_Often:
	        		newObs.setHumanFeed("OFTEN");
	        		break;
	        	case R.id.human_Regular:
	        		newObs.setHumanFeed("REGULAR");
	        		break;
	        	default:
	//        		Toast.makeText(context, "Error Human Feed Group", 2000).show();
	        		break;
	        	}    
	        }
	    });
	    
	    garbage_Feed_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.garbage_Never:
	        		newObs.setGarbageFeed("NEVER");
	        		break;
	        	case R.id.garbage_Rare:
	        		newObs.setGarbageFeed("RARE");
	        		break;
	        	case R.id.garbage_Often:
	        		newObs.setGarbageFeed("OFTEN");
	        		break;
	        	case R.id.garbage_Regular:
	        		newObs.setGarbageFeed("REGULAR");
	        		break;
	        	default:
	  //      		Toast.makeText(context, "Error Garbage Feed Group", 2000).show();
	        		break;
	        	}    
	        }
	    });
	    
	    plant_Feed_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.trees_Never:
	        		newObs.setPlantFeed("NEVER");
	        		break;
	        	case R.id.trees_Rare:
	        		newObs.setPlantFeed("RARE");
	        		break;
	        	case R.id.trees_Often:
	        		newObs.setPlantFeed("OFTEN");
	        		break;
	        	case R.id.trees_Regular:
	        		newObs.setPlantFeed("REGULAR");
	        		break;
	        	default: 
	    //    		Toast.makeText(context, "Error Plant Feed Group", 2000).show();
	        		break;
	        	}    
	        }
	    });
	    
	    other_Feed_Group.setOnCheckedChangeListener(new OnCheckedChangeListener()
	    {
	        public void onCheckedChanged(RadioGroup buttonView, int checkedID)
	        {
	        	switch (checkedID){
	        	case R.id.other_Never:
	        		newObs.setOtherFeed("NEVER");
	        		break;
	        	case R.id.other_Rare:
	        		newObs.setOtherFeed("RARE");
	        		break;
	        	case R.id.other_Often:
	        		newObs.setOtherFeed("OFTEN");
	        		break;
	        	case R.id.other_Regular:
	        		newObs.setOtherFeed("REGULAR");
	        		break;
	        	default: 
	       // 		Toast.makeText(context, "Error Other Feed Group", 2000).show();
	        		break;
	        	}   
	        }
	    });
	
		
		


		
		
	  



	    infoIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation6.this, infoMenu.class);
		        startActivity(i);
			}	    	
	    });
	    
	    settingsIconButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(NewObservation6.this, settingsMenu.class);
		        startActivity(i);
			}	    	

	    });

	    
	  
	    nextObsButton6.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				newObs.setFeedComments(feedComments.getText().toString());
				Intent i = new Intent(NewObservation6.this, NewObservation7.class);
		        startActivity(i);
				//finish();				
			}
			
	    });

	 

	
     }
     	@Override
	protected void onStart(){
     	// for the dialog indicating the gps is disabled
		super.onStart();
		/*
 		Calendar calendar = new GregorianCalendar();
 	    int hour = calendar.get(Calendar.HOUR); 		
 	    if(hour>18 || hour<10)
 		{
 			LinearLayout l = (LinearLayout) findViewById(R.id.Screen);
 			int color=Color.WHITE;
 			l.setBackgroundColor(color);
 			
 		}
		*/
	}
	@Override
	protected void onDestroy() {
		//stopListening();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		//stopListening();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		//startListening();
		super.onResume();
	}
	

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_MENU) ) {
	        
	    }
	    return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	super.onCreateOptionsMenu(menu);

	menu.add(PREFERENCES_GROUP_ID, PAGE_2, 0, "Date/Time")
	.setIcon(R.drawable.menuicondatetime);
	menu.add(PREFERENCES_GROUP_ID, PAGE_3, 0, "General")
	.setIcon(R.drawable.menuicongencom);
	menu.add(PREFERENCES_GROUP_ID, PAGE_4, 0, "Setting")
	.setIcon(R.drawable.menuiconsetting);
	menu.add(PREFERENCES_GROUP_ID, PAGE_5, 0, "Trees")
	.setIcon(R.drawable.menuicontree);
	menu.add(PREFERENCES_GROUP_ID, PAGE_6, 0, "Food")
	.setIcon(R.drawable.menuiconfood);
	menu.add(PREFERENCES_GROUP_ID, PAGE_7, 0, "Predators")
	.setIcon(R.drawable.menuiconpred);

	return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent i;
		switch (item.getItemId()) {
		case PAGE_2:
			i = new Intent(NewObservation6.this, NewObservation2.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_3:
			i = new Intent(NewObservation6.this, NewObservation3.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_4:
			i = new Intent(NewObservation6.this, NewObservation4.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		case PAGE_5:
			i = new Intent(NewObservation6.this, NewObservation5.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
	    case PAGE_6:
			return true;
		case PAGE_7:
			i = new Intent(NewObservation6.this, NewObservation7.class);
	        i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
	        startActivity(i);
	        break;
		}
		return true;
	}
	
}

